using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class FlyEnergy : MonoBehaviour
{
    public Slider greenFlyEnergy;
    public Slider orangeFlyEnergy;
    public Slider redFlyEnergy;

    float disEnergy;
    float realEnergy;


    public Vector3 firstPos;
    public bool isShake;
    float shakeAmount = 10;
    int count = 0;
    void Start()
    {
        disEnergy = 0;
    }

    void Update()
    {
        greenFlyEnergy.value = disEnergy;
        orangeFlyEnergy.value = disEnergy;
        redFlyEnergy.value = disEnergy;

        ChangeSlowly();
        ChangeColor();
    }
    void CostEnergy(int value)
    {
        if (realEnergy >= value)
        {
            realEnergy -= value;
        }
        else if (realEnergy == 0)
        {
            Shake();
        }
    }
    public void SetRealEnergy(float value)
    {
        realEnergy = value;
    }
    void AddEnergy(int value)
    {
        realEnergy += value;
    }
    void ChangeSlowly()
    {
        if (disEnergy != realEnergy)
        {
            ChangeEnergy();
        }
    }
    void ChangeEnergy()
    {
        if (disEnergy > realEnergy)
        {
            disEnergy -= 0.2f;
        }
        else if (disEnergy < realEnergy)
        {
            disEnergy += 0.2f;
        }
    }
    void ChangeColor()
    {
        if (disEnergy > 55)
        {
            greenFlyEnergy.gameObject.SetActive(true);
            orangeFlyEnergy.gameObject.SetActive(false);
            redFlyEnergy.gameObject.SetActive(false);

        }
        else if (disEnergy > 25 && disEnergy <= 55)
        {
            greenFlyEnergy.gameObject.SetActive(false);
            orangeFlyEnergy.gameObject.SetActive(true);
            redFlyEnergy.gameObject.SetActive(false);
        }
        else if (disEnergy >= 0 && disEnergy <= 25)
        {
            greenFlyEnergy.gameObject.SetActive(false);
            orangeFlyEnergy.gameObject.SetActive(false);
            redFlyEnergy.gameObject.SetActive(true);
        }
    }
    void Shake()
    {
        isShake = true;
        Vector3 pos = firstPos + Random.insideUnitSphere * shakeAmount;
        pos.y = transform.localPosition.y;
        transform.localPosition = pos;
        count++;
        if (count == 50 || disEnergy > 1)
        {
            EndShake();
        }
    }
    void EndShake()
    {
        isShake = false;
        firstPos.y = transform.localPosition.y;
        transform.localPosition = firstPos;
        count = 0;
    }
}
